﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.Graphics;
using Terraria.IO;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.Social;
using Terraria.UI;

namespace Terraria.GameContent.UI.Elements
{
	// Token: 0x020003E5 RID: 997
	public class UICharacterListItem : UIPanel
	{
		// Token: 0x17000287 RID: 647
		// (get) Token: 0x0600235B RID: 9051 RVA: 0x00019016 File Offset: 0x00017216
		public bool IsFavorite
		{
			get
			{
				return this._data.IsFavorite;
			}
		}

		// Token: 0x0600235C RID: 9052 RVA: 0x00477368 File Offset: 0x00475568
		public UICharacterListItem(PlayerFileData data, int snapPointIndex)
		{
			this.BorderColor = new Color(89, 116, 213) * 0.7f;
			this._dividerTexture = TextureManager.Load("Images/UI/Divider");
			this._innerPanelTexture = TextureManager.Load("Images/UI/InnerPanelBackground");
			this._buttonCloudActiveTexture = TextureManager.Load("Images/UI/ButtonCloudActive");
			this._buttonCloudInactiveTexture = TextureManager.Load("Images/UI/ButtonCloudInactive");
			this._buttonFavoriteActiveTexture = TextureManager.Load("Images/UI/ButtonFavoriteActive");
			this._buttonFavoriteInactiveTexture = TextureManager.Load("Images/UI/ButtonFavoriteInactive");
			this._buttonPlayTexture = TextureManager.Load("Images/UI/ButtonPlay");
			this._buttonDeleteTexture = TextureManager.Load("Images/UI/ButtonDelete");
			this._errorTexture = Texture2D.FromStream(Main.instance.GraphicsDevice, Assembly.GetExecutingAssembly().GetManifestResourceStream("Terraria.ModLoader.UI.ButtonError.png"));
			this.Height.Set(96f, 0f);
			this.Width.Set(0f, 1f);
			base.SetPadding(6f);
			this._data = data;
			this._playerPanel = new UICharacter(data.Player);
			this._playerPanel.Left.Set(4f, 0f);
			this._playerPanel.OnDoubleClick += this.PlayGame;
			base.OnDoubleClick += this.PlayGame;
			base.Append(this._playerPanel);
			UIImageButton uiimageButton = new UIImageButton(this._buttonPlayTexture);
			uiimageButton.VAlign = 1f;
			uiimageButton.Left.Set(4f, 0f);
			uiimageButton.OnClick += this.PlayGame;
			uiimageButton.OnMouseOver += this.PlayMouseOver;
			uiimageButton.OnMouseOut += this.ButtonMouseOut;
			base.Append(uiimageButton);
			UIImageButton uiimageButton2 = new UIImageButton(this._data.IsFavorite ? this._buttonFavoriteActiveTexture : this._buttonFavoriteInactiveTexture);
			uiimageButton2.VAlign = 1f;
			uiimageButton2.Left.Set(28f, 0f);
			uiimageButton2.OnClick += this.FavoriteButtonClick;
			uiimageButton2.OnMouseOver += this.FavoriteMouseOver;
			uiimageButton2.OnMouseOut += this.ButtonMouseOut;
			uiimageButton2.SetVisibility(1f, this._data.IsFavorite ? 0.8f : 0.4f);
			base.Append(uiimageButton2);
			if (SocialAPI.Cloud != null)
			{
				UIImageButton uiimageButton3 = new UIImageButton(this._data.IsCloudSave ? this._buttonCloudActiveTexture : this._buttonCloudInactiveTexture);
				uiimageButton3.VAlign = 1f;
				uiimageButton3.Left.Set(52f, 0f);
				uiimageButton3.OnClick += this.CloudButtonClick;
				uiimageButton3.OnMouseOver += this.CloudMouseOver;
				uiimageButton3.OnMouseOut += this.ButtonMouseOut;
				base.Append(uiimageButton3);
				uiimageButton3.SetSnapPoint("Cloud", snapPointIndex, null, null);
			}
			UIImageButton uiimageButton4 = new UIImageButton(this._buttonDeleteTexture);
			uiimageButton4.VAlign = 1f;
			uiimageButton4.HAlign = 1f;
			uiimageButton4.OnClick += this.DeleteButtonClick;
			uiimageButton4.OnMouseOver += this.DeleteMouseOver;
			uiimageButton4.OnMouseOut += this.DeleteMouseOut;
			this._deleteButton = uiimageButton4;
			if (!this._data.IsFavorite)
			{
				base.Append(uiimageButton4);
			}
			if (data.customDataFail != null)
			{
				UIImageButton uiimageButton5 = new UIImageButton(this._errorTexture);
				uiimageButton5.VAlign = 1f;
				uiimageButton5.HAlign = 1f;
				uiimageButton5.Left.Set(-24f, 0f);
				uiimageButton5.OnClick += this.ErrorButtonClick;
				uiimageButton5.OnMouseOver += this.ErrorMouseOver;
				uiimageButton5.OnMouseOut += this.DeleteMouseOut;
				base.Append(uiimageButton5);
			}
			if (data.Player.usedMods != null)
			{
				List<string> list = data.Player.usedMods.Except(ModLoader.GetLoadedMods()).ToList<string>();
				List<string> list2 = ModLoader.GetLoadedMods().Except(new string[]
				{
					"ModLoader"
				}).Except(data.Player.usedMods).ToList<string>();
				if (list.Count > 0 || list2.Count > 0)
				{
					UIText warningLabel = new UIText("", 1f, false)
					{
						VAlign = 0f,
						HAlign = 1f
					};
					warningLabel.Left.Set(-30f, 0f);
					warningLabel.Top.Set(3f, 0f);
					base.Append(warningLabel);
					UIImageButton uiimageButton6 = new UIImageButton(this._errorTexture)
					{
						VAlign = 0f,
						HAlign = 1f
					};
					uiimageButton6.Top.Set(-2f, 0f);
					StringBuilder stringBuilder = new StringBuilder(Language.GetTextValue("tModLoader.ModsDifferentSinceLastPlay"));
					StringBuilder stringBuilder2 = new StringBuilder();
					if (list.Count > 0)
					{
						stringBuilder2.Append((list.Count > 1) ? Language.GetTextValue("tModLoader.MissingXMods", list.Count) : Language.GetTextValue("tModLoader.Missing1Mod"));
						stringBuilder.Append("\n" + Language.GetTextValue("tModLoader.MissingModsListing", string.Join("\n", from x in list
						select "- " + x)));
					}
					if (list2.Count > 0)
					{
						stringBuilder2.Append(" " + ((list2.Count > 1) ? Language.GetTextValue("tModLoader.NewXMods", list2.Count) : Language.GetTextValue("tModLoader.New1Mod")));
						stringBuilder.Append("\n" + Language.GetTextValue("tModLoader.NewModsListing", string.Join("\n", from x in list2
						select "- " + x)));
					}
					string warning = stringBuilder2.ToString();
					string fullWarning = stringBuilder.ToString();
					uiimageButton6.OnMouseOver += delegate(UIMouseEvent a, UIElement b)
					{
						warningLabel.SetText(warning);
					};
					uiimageButton6.OnMouseOut += delegate(UIMouseEvent a, UIElement b)
					{
						warningLabel.SetText("");
					};
					uiimageButton6.OnClick += delegate(UIMouseEvent a, UIElement b)
					{
						Interface.infoMessage.SetMessage(fullWarning);
						Interface.infoMessage.SetGotoMenu(888, Main._characterSelectMenu);
						Main.menuMode = 10013;
					};
					base.Append(uiimageButton6);
				}
			}
			this._buttonLabel = new UIText("", 1f, false);
			this._buttonLabel.VAlign = 1f;
			this._buttonLabel.Left.Set(80f, 0f);
			this._buttonLabel.Top.Set(-3f, 0f);
			base.Append(this._buttonLabel);
			this._deleteButtonLabel = new UIText("", 1f, false);
			this._deleteButtonLabel.VAlign = 1f;
			this._deleteButtonLabel.HAlign = 1f;
			this._deleteButtonLabel.Left.Set((data.customDataFail == null) ? -30f : -54f, 0f);
			this._deleteButtonLabel.Top.Set(-3f, 0f);
			base.Append(this._deleteButtonLabel);
			uiimageButton.SetSnapPoint("Play", snapPointIndex, null, null);
			uiimageButton2.SetSnapPoint("Favorite", snapPointIndex, null, null);
			uiimageButton4.SetSnapPoint("Delete", snapPointIndex, null, null);
		}

		// Token: 0x0600235D RID: 9053 RVA: 0x00019023 File Offset: 0x00017223
		private void FavoriteMouseOver(UIMouseEvent evt, UIElement listeningElement)
		{
			if (this._data.IsFavorite)
			{
				this._buttonLabel.SetText(Language.GetTextValue("UI.Unfavorite"));
				return;
			}
			this._buttonLabel.SetText(Language.GetTextValue("UI.Favorite"));
		}

		// Token: 0x0600235E RID: 9054 RVA: 0x0001905D File Offset: 0x0001725D
		private void CloudMouseOver(UIMouseEvent evt, UIElement listeningElement)
		{
			if (this._data.IsCloudSave)
			{
				this._buttonLabel.SetText(Language.GetTextValue("UI.MoveOffCloud"));
				return;
			}
			this._buttonLabel.SetText(Language.GetTextValue("UI.MoveToCloud"));
		}

		// Token: 0x0600235F RID: 9055 RVA: 0x00019097 File Offset: 0x00017297
		private void PlayMouseOver(UIMouseEvent evt, UIElement listeningElement)
		{
			this._buttonLabel.SetText(Language.GetTextValue("UI.Play"));
		}

		// Token: 0x06002360 RID: 9056 RVA: 0x000190AE File Offset: 0x000172AE
		private void DeleteMouseOver(UIMouseEvent evt, UIElement listeningElement)
		{
			this._deleteButtonLabel.SetText(Language.GetTextValue("UI.Delete"));
		}

		// Token: 0x06002361 RID: 9057 RVA: 0x000190C5 File Offset: 0x000172C5
		private void ErrorMouseOver(UIMouseEvent evt, UIElement listeningElement)
		{
			this._deleteButtonLabel.SetText(this._data.customDataFail.modName + " Error");
		}

		// Token: 0x06002362 RID: 9058 RVA: 0x000190EC File Offset: 0x000172EC
		private void DeleteMouseOut(UIMouseEvent evt, UIElement listeningElement)
		{
			this._deleteButtonLabel.SetText("");
		}

		// Token: 0x06002363 RID: 9059 RVA: 0x000190FE File Offset: 0x000172FE
		private void ButtonMouseOut(UIMouseEvent evt, UIElement listeningElement)
		{
			this._buttonLabel.SetText("");
		}

		// Token: 0x06002364 RID: 9060 RVA: 0x00477B8C File Offset: 0x00475D8C
		private void CloudButtonClick(UIMouseEvent evt, UIElement listeningElement)
		{
			if (this._data.IsCloudSave)
			{
				this._data.MoveToLocal();
			}
			else
			{
				this._data.MoveToCloud();
			}
			((UIImageButton)evt.Target).SetImage(this._data.IsCloudSave ? this._buttonCloudActiveTexture : this._buttonCloudInactiveTexture);
			if (this._data.IsCloudSave)
			{
				this._buttonLabel.SetText(Language.GetTextValue("UI.MoveOffCloud"));
				return;
			}
			this._buttonLabel.SetText(Language.GetTextValue("UI.MoveToCloud"));
		}

		// Token: 0x06002365 RID: 9061 RVA: 0x00477C24 File Offset: 0x00475E24
		private void DeleteButtonClick(UIMouseEvent evt, UIElement listeningElement)
		{
			for (int i = 0; i < Main.PlayerList.Count; i++)
			{
				if (Main.PlayerList[i] == this._data)
				{
					Main.PlaySound(10, -1, -1, 1, 1f, 0f);
					Main.selectedPlayer = i;
					Main.menuMode = 5;
					return;
				}
			}
		}

		// Token: 0x06002366 RID: 9062 RVA: 0x00019110 File Offset: 0x00017310
		private void PlayGame(UIMouseEvent evt, UIElement listeningElement)
		{
			if (listeningElement != evt.Target)
			{
				return;
			}
			if (this._data.Player.loadStatus == 0)
			{
				Main.SelectPlayer(this._data);
			}
		}

		// Token: 0x06002367 RID: 9063 RVA: 0x00477C7C File Offset: 0x00475E7C
		private void FavoriteButtonClick(UIMouseEvent evt, UIElement listeningElement)
		{
			this._data.ToggleFavorite();
			((UIImageButton)evt.Target).SetImage(this._data.IsFavorite ? this._buttonFavoriteActiveTexture : this._buttonFavoriteInactiveTexture);
			((UIImageButton)evt.Target).SetVisibility(1f, this._data.IsFavorite ? 0.8f : 0.4f);
			if (this._data.IsFavorite)
			{
				this._buttonLabel.SetText(Language.GetTextValue("UI.Unfavorite"));
				base.RemoveChild(this._deleteButton);
			}
			else
			{
				this._buttonLabel.SetText(Language.GetTextValue("UI.Favorite"));
				base.Append(this._deleteButton);
			}
			UIList uilist = this.Parent.Parent as UIList;
			if (uilist != null)
			{
				uilist.UpdateOrder();
			}
		}

		// Token: 0x06002368 RID: 9064 RVA: 0x00019139 File Offset: 0x00017339
		private void ErrorButtonClick(UIMouseEvent evt, UIElement listeningElement)
		{
			ErrorLogger.LogException(this._data.customDataFail.InnerException, "The game has crashed!");
		}

		// Token: 0x06002369 RID: 9065 RVA: 0x00477D58 File Offset: 0x00475F58
		public override int CompareTo(object obj)
		{
			UICharacterListItem uicharacterListItem = obj as UICharacterListItem;
			if (uicharacterListItem == null)
			{
				return base.CompareTo(obj);
			}
			if (this.IsFavorite && !uicharacterListItem.IsFavorite)
			{
				return -1;
			}
			if (!this.IsFavorite && uicharacterListItem.IsFavorite)
			{
				return 1;
			}
			if (this._data.Name.CompareTo(uicharacterListItem._data.Name) != 0)
			{
				return this._data.Name.CompareTo(uicharacterListItem._data.Name);
			}
			return this._data.GetFileName(true).CompareTo(uicharacterListItem._data.GetFileName(true));
		}

		// Token: 0x0600236A RID: 9066 RVA: 0x000132A0 File Offset: 0x000114A0
		public override void MouseOver(UIMouseEvent evt)
		{
			base.MouseOver(evt);
			this.BackgroundColor = new Color(73, 94, 171);
			this.BorderColor = new Color(89, 116, 213);
		}

		// Token: 0x0600236B RID: 9067 RVA: 0x0042C064 File Offset: 0x0042A264
		public override void MouseOut(UIMouseEvent evt)
		{
			base.MouseOut(evt);
			this.BackgroundColor = new Color(63, 82, 151) * 0.7f;
			this.BorderColor = new Color(89, 116, 213) * 0.7f;
		}

		// Token: 0x0600236C RID: 9068 RVA: 0x00477DF4 File Offset: 0x00475FF4
		private void DrawPanel(SpriteBatch spriteBatch, Vector2 position, float width)
		{
			spriteBatch.Draw(this._innerPanelTexture, position, new Rectangle?(new Rectangle(0, 0, 8, this._innerPanelTexture.Height)), Color.White);
			spriteBatch.Draw(this._innerPanelTexture, new Vector2(position.X + 8f, position.Y), new Rectangle?(new Rectangle(8, 0, 8, this._innerPanelTexture.Height)), Color.White, 0f, Vector2.Zero, new Vector2((width - 16f) / 8f, 1f), SpriteEffects.None, 0f);
			spriteBatch.Draw(this._innerPanelTexture, new Vector2(position.X + width - 8f, position.Y), new Rectangle?(new Rectangle(16, 0, 8, this._innerPanelTexture.Height)), Color.White);
		}

		// Token: 0x0600236D RID: 9069 RVA: 0x00477ED8 File Offset: 0x004760D8
		protected override void DrawSelf(SpriteBatch spriteBatch)
		{
			base.DrawSelf(spriteBatch);
			CalculatedStyle innerDimensions = base.GetInnerDimensions();
			CalculatedStyle dimensions = this._playerPanel.GetDimensions();
			float num = dimensions.X + dimensions.Width;
			Utils.DrawBorderString(spriteBatch, this._data.Name, new Vector2(num + 6f, dimensions.Y - 2f), Color.White, 1f, 0f, 0f, -1);
			spriteBatch.Draw(this._dividerTexture, new Vector2(num, innerDimensions.Y + 21f), null, Color.White, 0f, Vector2.Zero, new Vector2((base.GetDimensions().X + base.GetDimensions().Width - num) / 8f, 1f), SpriteEffects.None, 0f);
			Vector2 vector = new Vector2(num + 6f, innerDimensions.Y + 29f);
			float num2 = 200f;
			Vector2 vector2 = vector;
			this.DrawPanel(spriteBatch, vector2, num2);
			spriteBatch.Draw(Main.heartTexture, vector2 + new Vector2(5f, 2f), Color.White);
			vector2.X += 10f + (float)Main.heartTexture.Width;
			Utils.DrawBorderString(spriteBatch, this._data.Player.statLifeMax2 + " HP", vector2 + new Vector2(0f, 3f), Color.White, 1f, 0f, 0f, -1);
			vector2.X += 65f;
			spriteBatch.Draw(Main.manaTexture, vector2 + new Vector2(5f, 2f), Color.White);
			vector2.X += 10f + (float)Main.manaTexture.Width;
			Utils.DrawBorderString(spriteBatch, this._data.Player.statManaMax2 + " MP", vector2 + new Vector2(0f, 3f), Color.White, 1f, 0f, 0f, -1);
			vector.X += num2 + 5f;
			Vector2 vector3 = vector;
			float num3 = 140f;
			if (GameCulture.Russian.IsActive)
			{
				num3 = 180f;
			}
			this.DrawPanel(spriteBatch, vector3, num3);
			string text = "";
			Color color = Color.White;
			switch (this._data.Player.difficulty)
			{
			case 0:
				text = Language.GetTextValue("UI.Softcore");
				break;
			case 1:
				text = Language.GetTextValue("UI.Mediumcore");
				color = Main.mcColor;
				break;
			case 2:
				text = Language.GetTextValue("UI.Hardcore");
				color = Main.hcColor;
				break;
			}
			vector3 += new Vector2(num3 * 0.5f - Main.fontMouseText.MeasureString(text).X * 0.5f, 3f);
			Utils.DrawBorderString(spriteBatch, text, vector3, color, 1f, 0f, 0f, -1);
			vector.X += num3 + 5f;
			Vector2 vector4 = vector;
			float num4 = innerDimensions.X + innerDimensions.Width - vector4.X;
			this.DrawPanel(spriteBatch, vector4, num4);
			TimeSpan playTime = this._data.GetPlayTime();
			int num5 = playTime.Days * 24 + playTime.Hours;
			string text2 = ((num5 < 10) ? "0" : "") + num5 + playTime.ToString("\\:mm\\:ss");
			vector4 += new Vector2(num4 * 0.5f - Main.fontMouseText.MeasureString(text2).X * 0.5f, 3f);
			Utils.DrawBorderString(spriteBatch, text2, vector4, Color.White, 1f, 0f, 0f, -1);
		}

		// Token: 0x04003FA8 RID: 16296
		private PlayerFileData _data;

		// Token: 0x04003FA9 RID: 16297
		private Texture2D _dividerTexture;

		// Token: 0x04003FAA RID: 16298
		private Texture2D _innerPanelTexture;

		// Token: 0x04003FAB RID: 16299
		private UICharacter _playerPanel;

		// Token: 0x04003FAC RID: 16300
		private UIText _buttonLabel;

		// Token: 0x04003FAD RID: 16301
		private UIText _deleteButtonLabel;

		// Token: 0x04003FAE RID: 16302
		private Texture2D _buttonCloudActiveTexture;

		// Token: 0x04003FAF RID: 16303
		private Texture2D _buttonCloudInactiveTexture;

		// Token: 0x04003FB0 RID: 16304
		private Texture2D _buttonFavoriteActiveTexture;

		// Token: 0x04003FB1 RID: 16305
		private Texture2D _buttonFavoriteInactiveTexture;

		// Token: 0x04003FB2 RID: 16306
		private Texture2D _buttonPlayTexture;

		// Token: 0x04003FB3 RID: 16307
		private Texture2D _buttonDeleteTexture;

		// Token: 0x04003FB4 RID: 16308
		private Texture2D _errorTexture;

		// Token: 0x04003FB5 RID: 16309
		private UIImageButton _deleteButton;
	}
}
